package com.haois.component.util.sdk;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * description
 *
 * @author RuiHaoZ on GCU
 * @date 2017/3/17 9:18
 */
public class CosJdk {

    private String bucketName;
    private String accessKey;
    private String secretKey;

    public CosJdk(String bucketName, String accessKey, String secretKey) {
        this.bucketName = bucketName;
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    private Logger logger = LoggerFactory.getLogger(CosJdk.class);

    public PutObjectResult upload(File cosFile, String filePath) {
        COSClient cosClient = getCOSClient();
        try {
            // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
            // 大文件上传请参照 API 文档高级 API 上传
            File localFile = cosFile;
            // 指定要上传到 COS 上的路径
            String key = filePath;
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            return putObjectResult;
        } catch (CosClientException cle) {
            cle.printStackTrace();
            logger.error(cle.getMessage());
            return null;
        }
    }

    public ObjectMetadata downLoad(File cosFile, String filePath) throws Exception {
        COSClient cosClient = getCOSClient();
        try {
            // 指定要下载到的本地路径
            File downFile = new File("src/test/resources/mydown.txt");
            // 指定要下载的文件所在的 bucket 和路径
            String key = filePath;
            GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
            ObjectMetadata downObjectMeta = cosClient.getObject(getObjectRequest, downFile);
            return downObjectMeta;
        } catch (CosClientException cle) {
            cle.printStackTrace();
            logger.error("downLoad object failed.", cle);
            return null;
        }
    }

    public void delete(File cosFile, String filePath) throws Exception {
        COSClient cosClient = getCOSClient();
        String key = filePath;
        try {
            cosClient.deleteObject(bucketName, key);
        } catch (CosClientException cle) {
            cle.printStackTrace();
            logger.error("delete object failed.", cle);
        }
    }

    public COSClient getCOSClient() {
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
        // 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        //广州（华南）	ap-guangzhou	<bucketname-APPID>.cos.ap-guangzhou.myqcloud.com
        ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
        // 3 生成cos客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        // bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
        return cosClient;
    }
}
